<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="1000" top="100px" destroy-on-close
		@closed="$emit('closed')">
		<el-form :model="FormData" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="130px">
			<el-form-item label="门店" prop="store">
				<Store v-model="FormData.store" multiple style="width:50%"></Store>
				<el-button type="primary" class="u-m-l-20" :disabled="levelList.length == 9"
					@click="addLevel">新增等级</el-button>
			</el-form-item>
			<el-form-item label="巡查等级配置" prop="level">
				<el-select v-model="FormData.level" placeholder="请选择巡查等级配置">
					<el-option v-for="(item,index) in unitList" :key=" index" :value="item.id" :label="item.name" />
				</el-select>
			</el-form-item>
			<el-form-item :label="'等级'+Number(index+1)" prop="" v-for="(item,index) in levelList" :key="index">
				<Users v-model="item.user" multiple style="width:50%"></Users>
				<el-button type="danger" icon="el-icon-delete" circle class="u-m-l-20"
					@click="deleteLevel(item,index)" />
			</el-form-item>

		</el-form>
		<template #footer>
			<el-button @click="visible=false">返 回</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				showAMap: false,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				fileurlArr: [],
				//表单数据
				FormData: {
					store: [],
					level: ''
				},
				levelList: [],
				//验证规则
				rules: {
					store: [{
						required: true,
						message: '请选择所属门店'
					}],
					level: [{
						required: true,
						message: '请选择巡查等级配置'
					}],

				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				},

				unitList: [],
				userInfo: {}
			}
		},
		watch: {

		},
		created() {
			this.userInfo = this.$TOOL.data.get("USER_INFO")
		},
		mounted() {
			this.getUnit()
		},
		methods: {
			//新增等级
			addLevel() {
				if (this.levelList.length == 9) {
					this.$message('最多只能新增9级！')
				} else {
					this.levelList.push({
						user: []
					})
				}
				console.log(1233, this.levelList)
			},
			// 删除等级
			deleteLevel(item, index) {
				this.levelList.splice(index, 1)
			},
			async getUnit() {
				let data = {
					page: 1,
					pagesize: 100
				}
				const res = await this.$API.api.level.list.post(data)
				if (res.data.datas) {
					this.unitList = res.data.datas
				}
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true

				delete this.FormData.createByName
				console.log(this.FormData)
				let _arr = []
				this.FormData.store.forEach(item => {
					_arr.push({
						id: item.id
					})
				})
				this.FormData.stores = _arr
				// 用户项
				let _users = this.levelList
				_users.forEach((items)=>{
					 let newArr = items.user.map((item) => {
					    return Object.assign({}, { id: item.id });
					  });
					items.user = newArr
				})

				console.log(333, this.levelList)
				let data ={
					stores:this.FormData.stores,
					level:{id:this.FormData.level},
					users:_users
				}
				const res = await this.$API.api.flow.save.post(data)
				this.isSaveing = false;
				if (res.code == 200) {
					this.$emit('success', this.FormData, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {
				this.FormData.id = data.id
				if (data.unit) {
					this.FormData.unit = data.unit.id
				}
				this.FormData.hitchType = data.hitchType
				this.FormData.linkTel = data.linkTel
				this.FormData.createByName = data.createByName
				this.FormData.describeText = data.describeText
				if (data.files) {
					let files = data.files.split(",")
					const newArray = files.map(item => {
						return {
							name: item,
							url: item
						}
					})
					this.FormData.files = newArray
					console.log(12333, newArray)
				}
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.FormData, data)
			}
		}
	}
</script>

<style>
</style>
